package com.cskaoyan.service;

import com.cskaoyan.bean.SearchHistory;
import com.cskaoyan.bean.SearchHistoryExample;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.vo.SearchHistoryVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchHistoryServicelmpl implements SearchHistoryService{

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    /**
     * 功能:搜索历史页面
     * 作者:whj
     */
    @Override
    public SearchHistoryVo query(Integer page, Integer limit, Integer userId, String keyword, String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);
        //排序
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.setOrderByClause(sort + " " + order);
        //根据userId,valueId进行精准查询
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if(userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if(keyword != null && !"".equals(keyword)){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        //符合条件的total
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();
        //返回结果
        SearchHistoryVo searchHistoryVo = new SearchHistoryVo();
        searchHistoryVo.setItems(searchHistories);
        searchHistoryVo.setTotal(total);
        return searchHistoryVo;
    }
}
